package com.kuang.job;

import com.kuang.mapper.*;
import com.kuang.mapper.pfsi.AstPfsiDeviceBranchMapper;
import com.kuang.mapper.pfsi.AstPfsiDeviceOpenMapper;
import com.kuang.mapper.pfsi.AstPfsiDeviceStateMsgMapper;
import com.kuang.pojo.*;
import com.kuang.pojo.pfsi.AstPfsiDeviceBranch;
import com.kuang.pojo.pfsi.AstPfsiDeviceOpen;
import com.kuang.pojo.pfsi.AstPfsiDeviceStateMsg;
import com.kuang.sync.managedown.ManageDownService;
import com.kuang.sync.pfsidown.PfsiDownService;
import com.kuang.sync.singledown.SingleDownService;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ScheduledService {

    Logger logger = Logger.getLogger(ScheduledService.class);
    @Resource
    AstControllerHeartMapper astControllerHeartMapper;
    @Resource
    AstManageDeviceHeartMapper astManageDeviceHeartMapper;
    @Resource
    AstImportControllerLogMapper astImportControllerLogMapper;
    @Resource
    AstUserCardRechargeMapper astUserCardRechargeMapper;
    @Resource
    ManageDownService manageDownService;
    @Resource
    AstUserCardActivateMapper astUserCardActivateMapper;
    @Resource
    AstControllerPoweronMapper astControllerPoweronMapper;
    @Resource
    SingleDownService singleDownService;
    @Resource
    AstLocalOpeningWellMapper astLocalOpeningWellMapper;
    @Resource
    AstRemoteOpeningWellMapper astRemoteOpeningWellMapper;
    @Resource
    AstLocalOpenWellMapper astLocalOpenWellMapper;
    @Resource
    AstLocalCloseWellMapper astLocalCloseWellMapper;
    @Resource
    AstPfsiDeviceOpenMapper astPfsiDeviceOpenMapper;
    @Resource
    PfsiDownService pfsiDownService;
    @Resource
    AstPfsiDeviceStateMsgMapper astPfsiDeviceStateMsgMapper;
    @Resource
    AstPfsiDeviceBranchMapper astPfsiDeviceBranchMapper;

    /*
    在一个特定的时间执行  Timer
    cron表达式
    秒 分 时 日 月 周几~
    @Scheduled(cron = "0 * * * * 0-7")
    0 8 16 * * ?    每天的16点8分0 秒执行一次
    0 0/5 16,17 * * ?  每天的16点与17点 每隔5分钟执行一次
    0 15 10 ？ * 1-6  每个月的周一到周六10点15分执行
    */

    /**
     * 执行删除控制器心跳信息数据
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void AstDeleteControllerHeartJob() {
        logger.info("执行删除控制器心跳信息数据开始");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        Date time = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String format = simpleDateFormat.format(time);
        int i = astControllerHeartMapper.deleteByExample(format);
        System.out.println("删除数据" + i);
        logger.info("执行删除控制器心跳信息数据结束");
    }

    /**
     * 执行删除管理机心跳信息数据
     */
    @Scheduled(cron = "0 5 23 * * ?")
    public void AstDeleteManageHeartJob() {
        logger.info("执行删除管理机心跳信息数据开始");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        Date time = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String format = simpleDateFormat.format(time);
        int i = astManageDeviceHeartMapper.deleteByExample(format);
        System.out.println("删除数据" + i);
        logger.info("执行删除管理机心跳信息数据结束");
    }

    /**
     * 执行删除接口日志数据
     */
    @Scheduled(cron = "0 0 6 * * ?")
    //@Scheduled(cron = "30 43 17 * * ?")
    public void AstImportControllerLogJob() {
        logger.info("执行删除接口日志数据开始");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        Date time = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String format = simpleDateFormat.format(time);
        int i = astImportControllerLogMapper.deleteByExample(format);
        System.out.println("删除数据" + i);
        logger.info("执行删除接口日志数据结束");
    }

    /**
     * 执行重新推送的定时任务
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void RepushController() {
        logger.info("重新推送接口数据开始");

        //iot推送管理机-用户卡充值给平台
        List<AstUserCardRecharge> astUserCardRecharges = astUserCardRechargeMapper.selectAllByFail();
        for (AstUserCardRecharge astUserCardRecharge : astUserCardRecharges) {
            manageDownService.postUserCardRecharge(astUserCardRecharge);
        }

        //iot推送管理机-用户卡开通给平台
        List<AstUserCardActivate> astUserCardActivates = astUserCardActivateMapper.selectAllByFail();
        for (AstUserCardActivate astUserCardActivate : astUserCardActivates) {
            manageDownService.postUserCardActivate(astUserCardActivate);
        }

        //iot推送控制器-设备开机给平台
        List<AstControllerPoweron> astControllerPowerons = astControllerPoweronMapper.selectAllByFail();
        for (AstControllerPoweron astControllerPoweron : astControllerPowerons) {
            singleDownService.postControllerPoweron(astControllerPoweron);
        }

        //iot推送控制器-（本地/远程）开井实时记录给平台
        List<AstLocalOpeningWell> astLocalOpeningWells = astLocalOpeningWellMapper.selectAllByFail();
        for (AstLocalOpeningWell astLocalOpeningWell : astLocalOpeningWells) {
            singleDownService.postRealTimeRecords("local",astLocalOpeningWell);
        }

        List<AstRemoteOpeningWell> astRemoteOpeningWells = astRemoteOpeningWellMapper.selectAllByFail();
        for (AstRemoteOpeningWell astRemoteOpeningWell : astRemoteOpeningWells) {
            singleDownService.postRealTimeRecords("remote",astRemoteOpeningWell);
        }

        //iot推送控制器-本地开井给平台
        for (AstLocalOpenWell astLocalOpenWell : astLocalOpenWellMapper.selectAllByFail()) {
            singleDownService.postLocalOpen(astLocalOpenWell);
        }

        //iot推送控制器-本地关井给平台
        for (AstLocalCloseWell astLocalCloseWell : astLocalCloseWellMapper.selectAllByFail()) {
            singleDownService.postLocalClose(astLocalCloseWell);
        }

        //iot推送水肥一体机-设备端开机给平台
        for (AstPfsiDeviceOpen astPfsiDeviceOpen : astPfsiDeviceOpenMapper.selectAllByFail()) {
            pfsiDownService.postPfsiDeviceOpen(astPfsiDeviceOpen);
        }

        //iot推送水肥一体机-设备状态信息给平台
        for (AstPfsiDeviceStateMsg astPfsiDeviceStateMsg : astPfsiDeviceStateMsgMapper.selectAllByFail()) {
            pfsiDownService.postPfsiDeviceStateMsg(astPfsiDeviceStateMsg);
        }

        //iot推送水肥一体机-肥通支路信息给平台
        for (AstPfsiDeviceBranch astPfsiDeviceBranch : astPfsiDeviceBranchMapper.selectAllByFail()) {
            pfsiDownService.postPfsiDeviceBranch(astPfsiDeviceBranch);
        }

        logger.info("重新推送接口数据结束");
    }

    /**
     * 执行删除控制器本地开井实时记录
     */
    @Scheduled(cron = "0 10 23 * * ?")
    public void AstLocalOpeningWellJob() {
        logger.info("执行删除控制器本地开井实时记录开始");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -2);
        Date time = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String format = simpleDateFormat.format(time);
        int i = astLocalOpeningWellMapper.deleteByExample(format);
        System.out.println("删除数据" + i);
        logger.info("执行删除控制器本地开井实时记录结束");
    }

}
